using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;

namespace DAL
{
   public class tbdiadiemDAL
    {
           private SqlConnection cn = null;
        public tbdiadiemDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
         public tbdiadiemDAL(tbdiadiemDAL thach)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public void Insert(tbdiadiem diadiem)
        {
            SqlCommand cm = new SqlCommand("diadiem_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@tendiadiem", diadiem.Tendiadiem);
            cm.Parameters.AddWithValue("@diachi", diadiem.Diachi);
            cm.Parameters.AddWithValue("@sodienthoai", diadiem.Sodienthoai);
            cm.Parameters.AddWithValue("@email", diadiem.Email);
            cm.ExecuteNonQuery();
        }
        public void Update(tbdiadiem diadiem)
        {
            SqlCommand cm = new SqlCommand("diadiem_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madiadiem", diadiem.Madiadiem);
            cm.Parameters.AddWithValue("@tendiadiem",diadiem.Tendiadiem);
            cm.Parameters.AddWithValue("@diachi", diadiem.Diachi);
            cm.Parameters.AddWithValue("@Sodienthoai", diadiem.Sodienthoai);
            cm.Parameters.AddWithValue("@email", diadiem.Email);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("diadiem_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madiadiem", iD);
            cm.ExecuteNonQuery();
        }
        public tbdiadiemcollection Get(string where, int pageIndex, int pageSize,
           string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("diadiem_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbdiadiemcollection diadiem1Collection = new tbdiadiemcollection();
            while (dr.Read())
            {
                tbdiadiem diadiem = new tbdiadiem();
                diadiem.Madiadiem = int.Parse(dr.GetValue(0).ToString());
                diadiem.Tendiadiem = dr.GetValue(1).ToString();
                diadiem.Diachi = dr.GetValue(2).ToString();
                diadiem.Sodienthoai = dr.GetValue(3).ToString();
                diadiem.Email = dr.GetValue(4).ToString();
                diadiem1Collection.Add(diadiem);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return diadiem1Collection;
        }
    }
}
